package com.itheima.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtils {
    //1.私有化构造方法
    private MyBatisUtils (){};

    //2.声明 SqlSessionFactory 对象
    private static SqlSessionFactory factory;

    //3.加载配置文件并为factory赋值
    static {
        try {
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("MyBatisConfig.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

   public static SqlSession getSqlSession(){
       SqlSession sqlSession = factory.openSession();
       return sqlSession;
   }


}
